package mapEditor.obj;

import debug.*;

import mapEditor.svg.SVGDOMTree;
import mapEditor.utils.MapLocation;
import mapEditor.utils.SvgFill;
import mapEditor.utils.SvgStroke;
import mapEditor.utils.Util;

import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Dimension;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPanel;

import org.w3c.dom.svg.SVGEllipseElement;


// DOC: http://www.w3.org/TR/SVG11/shapes.html#EllipseElement
// DOC: http://www.w3.org/TR/SVG11/types.html#InterfaceSVGAnimatedLength
// DOC: http://www.zvon.org/xxl/svgReference/Output/el_ellipse.html
public class EllipseObject extends DrawObject
{ protected static final boolean USE_AnimatedLength=false; // OVERRIDE (to make updateSVGElement work)

  public EllipseObject(SVGDOMTree domTree,MapLocation location1,MapLocation location2)
  { super(domTree,domTree.appendSVGEllipseElement());
    stroke.setEnabled(true);
	add(location1);
	add(location2);
	this.setClosedPath(true);
  }
  
  public EllipseObject(SVGDOMTree domTree,SVGEllipseElement svgElement)
  { super(domTree,svgElement);
	decodeSVG(svgElement);
  }
  
  public void decodeSVG(SVGEllipseElement svgElement)
  { Debug.TRACE("EllipseObject.decodeSVG: svgElement="+svgElement);
    this.setIdent(svgElement.getAttribute("id"));
    this.fill=new SvgFill(svgElement);
    this.stroke=new SvgStroke(svgElement);
    clearPointSet();
//    double cx=Double.parseDouble(svgElement.getAttribute("cx"));
//    double cy=Double.parseDouble(svgElement.getAttribute("cy"));
//    double rx=Double.parseDouble(svgElement.getAttribute("rx"));
//    double ry=Double.parseDouble(svgElement.getAttribute("ry"));
    
//    SVGAnimatedLength alCx=svgElement.getCx();
//    SVGAnimatedLength alCy=svgElement.getCy();
//    SVGAnimatedLength alRx=svgElement.getRx();
//    SVGAnimatedLength alRy=svgElement.getRy();
//    SVGLength aCx=alCx.getAnimVal();
//    SVGLength aCy=alCy.getAnimVal();
//    SVGLength aRx=alRx.getAnimVal();
//    SVGLength aRy=alRy.getAnimVal();
//    double cx=aCx.getValue();
//    double cy=aCy.getValue();
//    double rx=aRx.getValue();
//    double ry=aRy.getValue();

    double cx=getValue(svgElement.getCx());
    double cy=getValue(svgElement.getCy());
    double rx=getValue(svgElement.getRx());
    double ry=getValue(svgElement.getRy());

//    double x0=cx-rx;
//    double y0=cy-ry;
//    double x1=cx+rx;
//    double y1=cy+ry;

//    add(MapLocation.newPixelPoint(domTree.getMapGrid(),x0,y0));
//    add(MapLocation.newPixelPoint(domTree.getMapGrid(),x1,y1));
    add(MapLocation.newPixelPoint(domTree.getMapGrid(),cx-rx,cy-ry));
    add(MapLocation.newPixelPoint(domTree.getMapGrid(),cx+rx,cy+ry));
    setClosedPath(true);
  }

  protected Shape getShape(double dx,double dy)
  { MapLocation p0=getMapLocation(0);
    MapLocation p1=getMapLocation(1);
    double x0=p0.getX()+dx;
    double y0=p0.getY()+dy;
    double x1=p1.getX()+dx;
    double y1=p1.getY()+dy;
    double x=Math.min(x0,x1);
    double y=Math.min(y0,y1);
    double width=Math.abs(x1-x0);
    double height=Math.abs(y1-y0);
    Ellipse2D shape=new Ellipse2D.Double(x,y,width,height);
    return(shape);	  
  }
  
  protected void directUpdateSVGElement()
  {	MapLocation p0=getMapLocation(0);
    MapLocation p1=getMapLocation(1);
    double x0=p0.getX();
    double y0=p0.getY();
    double x1=p1.getX();
    double y1=p1.getY();
    double x=Math.min(x0,x1);
    double y=Math.min(y0,y1);
    double width=Math.abs(x1-x0);
    double height=Math.abs(y1-y0);
    
    double rx=width/2;
    double ry=height/2;
    double cx=x+rx;
    double cy=y+ry;
    svgElement.setAttributeNS(null,"id",getIdent());
    
    if(USE_AnimatedLength)
    { SVGEllipseElement ellipse=(SVGEllipseElement)svgElement;
      setValue(ellipse.getCx(),cx);
      setValue(ellipse.getCy(),cy);
      setValue(ellipse.getRx(),rx);
      setValue(ellipse.getRy(),ry);
    }
    else
    { svgElement.setAttributeNS(null,"cx",""+cx);
      svgElement.setAttributeNS(null,"cy",""+cy);
      svgElement.setAttributeNS(null,"rx",""+rx);
      svgElement.setAttributeNS(null,"ry",""+ry);	
    }
    fill.appendSVGAttributes(svgElement);
    stroke.appendSVGAttributes(svgElement);
  }
  
  
  // ****************************************************************
  // *** INNER CLASS: EditGeneralPanel
  // ****************************************************************
  public JPanel getEditGeneralPanel(String title)
  { return(new EditGeneralPanel(title)); }
  private class EditGeneralPanel extends JPanel
  { static final long serialVersionUID=0; // AD'HOC
    private JTextField identField;
    private JButton strokeButton;
    private JButton fillButton;
    
    public void paint(Graphics g)
    { Debug.TRACE("EllipseObject.EditGeneralPanel.paint: ");
      identField.setText(getIdent());
      strokeButton.setBackground(stroke.getColor());
      fillButton.setBackground(fill.getColor());
  	super.paint(g);  
    }
  	
    public EditGeneralPanel(String title)
    { setBorder(Util.getTitleBorder(title));
  	  setPreferredSize(new Dimension(350,120));
      setLayout(new GridLayout(5,2));
      add(new JLabel("       Ident:"));
      identField=new JTextField(getIdent());
      identField.addActionListener(new ActionListener()
      { public void actionPerformed(ActionEvent e)
        { setIdent(identField.getText()); }
      });
      add(identField);
      fillButton=addEditFillButton(this,true);
      strokeButton=addEditStrokeButton(this,true);
    }
 }

}
